package com.itzhigui.controller.admin;

import com.itzhigui.entity.User;
import com.itzhigui.service.UserService;
import com.itzhigui.utils.JWTUtil;
import com.itzhigui.utils.ResponseUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台调用的接口
 *
 * @author java1412
 * @since 2023-11-09 14:29:09
 */
@RestController
@Slf4j
//@CrossOrigin
@RequestMapping(value = "/admin/user")
@Api(value = "后台用户接口", tags = "AdminUserController") // 对控制器的描述
public class AdminUserController {

    @Resource
    private UserService userService;

    // 在swagger2中对接口的描述，value属性是简单的描述，notes：详细描述
    @ApiOperation(value = "获取所有用户", notes = "后台获取所有的用户详细信息")
//    @ApiImplicitParams(
//            value = {
//                    @ApiImplicitParam(name = "page", value = "第几页", required = true, defaultValue = "1", format = "1", dataType = "java.lang.Integer", paramType = "path"),
//                    @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, defaultValue = "1", format = "2", dataType = "java.lang.Integer", paramType = "path")
//            }
//    )
    @GetMapping(value = "/find/{page}/{pageSize}")
    public ResponseUtil find(
            @PathVariable(value = "page") @ApiParam(name = "page", value = "page", required = true, defaultValue = "1") Integer page,
            @PathVariable(value = "pageSize") @ApiParam(name = "pageSize", value = "pageSize", required = true, defaultValue = "3") Integer pageSize
    ) {
        return userService.find(page, pageSize);
    }

    @ApiOperation(value = "添加用户", notes = "后台添加用户信息")
    @ApiImplicitParam(name = "user", value = "用户对象数据", required = true, type = "com.itzhigui.entity.User")
    @PostMapping(value = "/add")
    public ResponseUtil add(@RequestBody User user) {
        return userService.add(user);
    }

    // 编写后台的登录接口
    @ApiOperation(value = "后台登录", notes = "后台登录接口")
    @ApiImplicitParam(name = "user", value = "用户对象数据", required = true, type = "com.itzhigui.entity.User")
    @PostMapping(value = "/login")
    public Map<String, Object> login(
            @RequestBody User user
    ) {
        // return userService.findAdminByName(user);
        ResponseUtil r = userService.findAdminByName(user);
        Map<String, Object> map = new HashMap<>();
        if (r.getCode().intValue() == 1012) {
            // 登录成功
            User loginUser = (User) r.getData();
            // 生成jwt令牌
            Map<String, String> payload = new HashMap<>();
            payload.put("userId", loginUser.getId().toString());
            payload.put("username", loginUser.getUsername());
            String token = JWTUtil.getToken(payload);
            map.put("stats", true);
            map.put("msg", "认证成功");
            map.put("username", loginUser.getUsername());
            map.put("token", token);
        } else {
            // 登录失败
            map.put("stats", false);
            map.put("msg", r.getMsg());
        }
        return map;
    }

    // 测试令牌接口
//    @GetMapping(value = "/test")
//    public Map<String, Object> test(String token) {
//        Map<String, Object> map = new HashMap<>();
//        try {
//            DecodedJWT decodedJWT = JWTUtil.verify(token);
//            // 没有异常，验签是通过的
//            map.put("state", true);
//            map.put("msg", "请求成功");
//            return map;
//        } catch (SignatureVerificationException e) {
//            e.printStackTrace();
//            map.put("msg", "无效签名!");
//        } catch (TokenExpiredException e) {
//            e.printStackTrace();
//            map.put("msg", "token过期!");
//        } catch (AlgorithmMismatchException e) {
//            e.printStackTrace();
//            map.put("msg", "token算法不一致!");
//        } catch (Exception e) {
//            e.printStackTrace();
//            map.put("msg", "token无效");
//        }
//        map.put("stats", false);
//        return map;
//    }

    @GetMapping("/test")
    public Map<String, Object> test(String token) {
        Map<String, Object> map = new HashMap<>();
        map.put("state", true);
        map.put("msg", "请求成功");
        return map;
    }
}
